package generates

import (
	"bytes"
	"encoding/base64"
	"strings"

	"github.com/devfeel/dotweb/framework/crypto"

)

// NewAuthorizeGenerate create to generate the authorize code instance
func NewAuthorizeGenerate() *AuthorizeGenerate {
	return &AuthorizeGenerate{}
}

// AuthorizeGenerate generate the authorize code
type AuthorizeGenerate struct{}


func (ag *AuthorizeGenerate) Token(data *GenerateBasic) (code string, err error) {
	buf := bytes.NewBufferString(data.ClientID)
	buf.WriteString(data.ClientSecret)

	token := cryptos.GetMd5String(buf.String())
	code = base64.URLEncoding.EncodeToString([]byte(token))
	code = strings.ToUpper(strings.TrimRight(code, "="))

	return
}